<?php
/**
 * Created by PhpStorm.
 * User: eugene
 * Date: 2018/3/15
 * Time: 14:31
 */

namespace App\Services;

use App\Models\Member;
use App\Models\MyCar;
use App\Models\MyStage;
use App\Models\Parking;
use App\Models\Record;
use App\Repos\CarRepo;
use App\Repos\MemberRepo;
use App\Repos\MessageRepo;
use Carbon\Carbon;

class StopCar
{
    
    public $action_info,$memberRepo;
    
    protected
        $user,      //主要操作者
        $to_user,   //被操作者
        $car,       //操作者的车
        $myCar,     //操作者的唯一车
        $to_car,    //被操作者的车
        $to_MyCar,    //被操作者的唯一车
        $parking;   //所操作的车位
    
    public function __construct(MemberRepo $memberRepo)
    {
        $this->memberRepo = $memberRepo;
    }
    
    public function __set($name,$value)
    {
        $this->$name = $value;
    }
    
    
    public function doStopIn()
    {
        //不能停在自己的车位
        if ($this->parking->user_id == $this->user->id){
            $this->action_info=[
                'code'=>-1,
                'message'=>'不能停在自己的车位',
            ];
            return false;
        }
        //获取上一个停车状态
        $old_record = Record::withoutGlobalScope('status')->where('ParkUid',$this->user->id)->where('myCarID',$this->myCar->ID)->first();
        
        $never_stoped_car = !$old_record || !$old_record->ParkWhoUid;//没有旧记录，说明从未停过车
        
        //上一次停车时间，如果空驶，就是上次购买时间
        $before_park_time = !$old_record ? $this->myCar->BuyTime : $old_record->ParkStartTime;
        
        //停了多久了  分钟 为单位
        //$parked_minutes = min(Carbon::now()->diffInMinutes(Carbon::parse($before_park_time)),config('car.cfgStopLimit.maxDriveTime'));
        
        $system_minutes = config('car.cfgStopLimit.stop');
    
        $last_park_time1 = Record::where('ParkUid',$this->user->id)->where('myCarID',$this->myCar->ID)->max('ParkStartTime');
        if($last_park_time1 && Carbon::now()->diffInMinutes(Carbon::parse($last_park_time1)) < $system_minutes){
            $this->action_info=[
                'code'=>-1,
                'message'=>$system_minutes.'分钟内不能再次挪车',
            ];
            return false;
        }
        
        $stopOneParking = config('car.cfgStopLimit.stopOneParking');
        //前状态非空驶 & 不能在系统设置时间内更换车位
        $last_park_time2 = Record::where('ParkUid',$this->user->id)->where('ParkWhoUid',$this->to_user->id)->max('ParkStartTime');
        if($last_park_time2 && Carbon::now()->diffInMinutes(Carbon::parse($last_park_time2)) < $stopOneParking){
            $this->action_info=[
                'code'=>-1,
                'message'=>$stopOneParking.'分钟内不能再被同一个玩家名下的汽车停靠',
            ];
            return false;
        }
        
        //是否还有罚金需要缴纳
        $carRepo=new CarRepo();
        $carRepo->countStatus($this->myCar);
        if($carRepo->forfeit > 0){
            $this->action_info=[
                'code'=>-1,
                'message'=>'还有罚金未缴清',
            ];
            return false;
        }
        
        //不能更换同一人的车位
        //if($old_record->ParkWhoUid == $this->to_user->id){
        //    $this->action_info=[
        //        'code'=>-2,
        //        'message'=>'不能更换同一人的车位',
        //    ];
        //    return false;
        //}
        
        //如果是停在私家车位上，需要判断与对方是否关注
        if($this->parking->isPrivate()){
            
            if(!$this->memberRepo::checkIsFriend($this->user->id,$this->to_user->id)){
                $this->action_info=[
                    'code'=>-2,
                    'message'=>'你还不是对方的好友，暂时无法停放',
                ];
                return false;
            }
        }
        
        if($never_stoped_car){
            //从未停过车的逻辑
            if($old_record){
                $old_record->ParkWhoUid = $this->to_user->id;
                $old_record->Parklot = $this->parking->id;
                $old_record->ParkStartTime = Carbon::now()->toDateTimeString();
                $old_record->ParkStatus=1;
                $old_record->save();
            }else{
                $new_record = new Record();
                $new_record->CarID = $this->car->id;
                $new_record->myCarID = $this->myCar->ID;
                $new_record->ParkUid = $this->user->id;
                $new_record->ParkWhoUid = $this->to_user->id;
                $new_record->Parklot = $this->parking->id;
                $new_record->ParkStartTime = Carbon::now()->toDateTimeString();
                $new_record->ParkStatus=1;
                $new_record->save();
            }
        }else{
            //更换车位的逻辑
            if(Record::where('ParkWhoUid',$this->to_user->id)->where('Parklot',$this->parking->id)->exists()){
                $this->action_info=[
                    'code'=>-2,
                    'message'=>'车位已满',
                ];
                return false;
            }
            
            //更新停车记录
            $old_record->ParkWhoUid=$this->to_user->id;
            $old_record->Parklot=$this->parking->id;
            $old_record->ParkStartTime=Carbon::now()->toDateTimeString();
            $old_record->ParkStatus=1;
            $old_record->save();
    
            $this->myCar->ParkWhoUid=$this->to_user->id;
            $this->myCar->Parklot=$this->parking->id;
            $this->myCar->save();
        }
        
        //停入车位统一发消息
        $template = sprintf(config('words.messages.stop_car'),
            $this->user->nickname,
            Carbon::parse($before_park_time)->diffForHumans(null,true),
            $this->to_user->nickname
        );
        $messageBag=[
            'type'=>1,
            'user_id'=>$this->user->id,
            'to_user_id'=>$this->to_user->id,
            'text'=>$template,
            'record_id'=>$old_record ?$old_record->RID:$new_record->RID,
        ];
        MessageRepo::push($messageBag);
        
        $this->action_info=[
            'code'=>0,
            'message'=>'停车成功',
        ];
        return true;
    }
    
    public function punish()
    {
        //只能对其它人的车贴条
        if(!MyCar::where('uid',$this->to_user->id)->where('ID',$this->myCar->ID)->exists()){
            $this->action_info=[
                'code'=>-1,
                'message'=>'车辆信息不存在',
            ];
            return false;
        }
    
        //验证车在自己车位中
        $record = Record::where('ParkUid',$this->to_user->id)->where('ParkWhoUid',$this->user->id)->where('myCarID',$this->myCar->ID)->first();
        if(empty($record)){
            $this->action_info=[
                'code'=>-1,
                'message'=>'只能对自己车位中的车贴条',
            ];
            return false;
        }
        
        //只能对在私人车位中的车进行贴条
        
        $parking = Parking::find($record->Parklot);
        
        if($parking->isFree()){
            $this->action_info=[
                'code'=>-1,
                'message'=>'只能对不是免费车位中的车进行贴条',
            ];
            return false;
        }
    
        if($record->ParkPunish == 1){
            $this->action_info=[
                'code'=>-1,
                'message'=>'不要重复贴条',
            ];
            return false;
        }
    
        $carRepo = new CarRepo();
        $carRepo->countStatus($this->myCar);
        
        if($carRepo->status == config('car.car_status.valid')){
            //收益期内仅标志
            $record->ParkPunish=1;
            $record->save();
    
            $template = '将您停在其TA车位上的车贴条';
        }else{
            //结算收益并将车赶走
            //没收全部收入
            $income = $carRepo->current_income;
            
            $this->memberRepo->incrementUserCredit($this->user,$income);
            
            //重置停车时间 、状态
            $record->ParkStartTime=Carbon::now()->toDateTimeString();
            $record->ParkStatus=0;
            $record->save();
    
            //将此车的道具状态改为已使用
            MyStage::where('car_id',$this->myCar->ID)->where('uid',$this->to_user->id)->update(['status'=>1]);
    
            $template = sprintf('将您停在TA车位上的车贴条,并没收了您的%s元收入',$income);
        }
        
        
        $messageBag=[
            'type'=>2,
            'user_id'=>$this->user->id,
            'to_user_id'=>$this->to_user->id,
            'text'=>$template,
            'record_id'=>$record->RID,
        ];
        
        MessageRepo::push($messageBag);
    
        $this->action_info=[
            'code'=>0,
            'message'=>'贴条成功',
        ];
        return true;
        
    }
    
}